package hbi.as3.utils
{
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.utils.ByteArray;

	public class HttpOutput implements IFlushableOutput
	{
		
		private var _url:String;
		private var _buffer:ByteArray;
		
		public function HttpOutput(url:String)
		{
			this._url = url;
			this._buffer = new ByteArray();
		}
		
		public function flush():void
		{
			var buffer:ByteArray = _buffer;
			_buffer = new ByteArray();
			buffer.position = 0;
			
			var request:URLRequest = new URLRequest(_url);
			request.method = URLRequestMethod.POST;
			request.contentType = "application/octet-stream";
			request.data = buffer;
			var loader:URLLoader = new URLLoader();
			loader.load(request);
		}
		
		public function writeBytes(bytes:ByteArray, offset:uint=0.0, 
			length:uint=0.0):void
		{
			_buffer.writeBytes(bytes, offset, length);
		}
		
		public function writeBoolean(value:Boolean):void
		{
			_buffer.writeBoolean(value);
		}
		
		public function writeByte(value:int):void
		{
			_buffer.writeByte(value);
		}
		
		public function writeShort(value:int):void
		{
			_buffer.writeShort(value);
		}
		
		public function writeInt(value:int):void
		{
			_buffer.writeInt(value);
		}
		
		public function writeUnsignedInt(value:uint):void
		{
			_buffer.writeUnsignedInt(value);
		}
		
		public function writeFloat(value:Number):void
		{
			_buffer.writeFloat(value);
		}
		
		public function writeDouble(value:Number):void
		{
			_buffer.writeDouble(value);
		}
		
		public function writeMultiByte(value:String, charSet:String):void
		{
			_buffer.writeMultiByte(value, charSet);
		}
		
		public function writeUTF(value:String):void
		{
			_buffer.writeUTF(value);
		}
		
		public function writeUTFBytes(value:String):void
		{
			_buffer.writeUTFBytes(value);
		}
		
		public function writeObject(object:*):void
		{
			_buffer.writeObject(object);
		}
		
		public function get objectEncoding():uint
		{
			return _buffer.objectEncoding;
		}
		
		public function set objectEncoding(version:uint):void
		{
			_buffer.objectEncoding = version;
		}
		
		public function get endian():String
		{
			return _buffer.endian;
		}
		
		public function set endian(type:String):void
		{
			_buffer.endian = type;
		}
		
	}
}
